package com.swz.dcrm.ui.approval;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.ApprovalAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.adpter.MultiItemTypeAdapter;
import com.swz.dcrm.digger.AppModule;
import com.swz.dcrm.digger.DaggerAppComponent;
import com.swz.dcrm.model.Approval;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.home.AddOrderFragmentArgs;
import com.swz.dcrm.ui.home.ClueDetailFragment;
import com.swz.dcrm.ui.home.ClueDetailFragmentArgs;
import com.swz.dcrm.ui.home.QuotationFragmentArgs;
import com.swz.dcrm.ui.viewmodel.ApprovalViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.wrapper.EmptyWrapper;
import com.xh.baselibrary.callback.OnClickListener;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class ApprovalItemFragment extends BaseFragment {


    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView rv;
    private int page=1;
    private int size=15;
    private long total;
    private int status;

    private ApprovalAdapter approvalAdapter;
    private EmptyWrapper emptyWrapper;
    @Inject
    ApprovalViewModel approvalViewModel;
    @Inject
    MainViewModel mainViewModel;
    public static ApprovalItemFragment newInstance(int status,boolean isDeal) {
        ApprovalItemFragment fragment=new ApprovalItemFragment();
        Bundle bundle=new Bundle();
        bundle.putInt("status",status);
        bundle.putBoolean("isDeal",isDeal);
        fragment.setArguments(bundle);
        return fragment;
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }

         boolean  isDeal=getArguments().getBoolean("isDeal");
        MediatorLiveData<PageResponse<Approval>> mediatorLiveData;
        if(mainViewModel.isManager()){
            mediatorLiveData=approvalViewModel.getApprovals(status,page,size,isDeal);
        }else {
            mediatorLiveData=approvalViewModel.getApprovals(null,page,size,isDeal);

        }
        mediatorLiveData.observe(this, observer);

    }


    Observer<PageResponse<Approval>> observer =new Observer<PageResponse<Approval>>() {
        @Override
        public void onChanged(PageResponse<Approval> approvalPageResponse) {
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadmore();


            if(approvalPageResponse.isSuccess()){
                total=approvalPageResponse.getTotal();
                if(approvalAdapter==null){
                    approvalAdapter=new ApprovalAdapter(getContext(),approvalPageResponse.getData(),mainViewModel.isManager());
                    approvalAdapter.setOnButtonClickListener(onButtonClickListener);
                    emptyWrapper=getEmptyWrapper(approvalAdapter,R.mipmap.empty_approval);
                    rv.setAdapter(emptyWrapper);
                }else {
                    approvalAdapter.refresh(approvalPageResponse.getPage(),approvalPageResponse.getData());

                    emptyWrapper.notifyDataSetChanged();
                }
            }
        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        page=1;
        onLoadRetry();
        smartRefreshLayout.setEnableLoadmore(true);
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_approval_item;
    }

    @Override
    public boolean initView() {
         DaggerAppComponent.builder().appModule(new AppModule(this))
                .build().inject(this);
        status=getArguments().getInt("status");

        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.addItemDecoration(new CustomDecoration(getContext(), 0, 10, 10, 10));
        smartRefreshLayout.setOnRefreshListener(refreshlayout -> {
            page=1;
            onLoadRetry();
            smartRefreshLayout.setEnableLoadmore(true);
        });
        smartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            if (approvalAdapter != null && approvalAdapter.getDatas().size() >= total) {
                smartRefreshLayout.finishLoadmore();
                smartRefreshLayout.setEnableLoadmore(false);
                return;
            } else {
                page++;
                onLoadRetry();
            }
        });
        return false;
    }



    ApprovalAdapter.OnButtonClickListener onButtonClickListener=new ApprovalAdapter.OnButtonClickListener() {
        @Override
        public void onApproval(Approval approval) {
            ApprovalOpinionFragmentArgs.Builder builder=new ApprovalOpinionFragmentArgs.Builder();
            builder.setApproval(new Gson().toJson(approval));
            builder.setType(approval.getApplyName());
            go(null,R.id.action_approvalFragment_to_approvalOpinionFragment,builder.build().toBundle());
        }

        @Override
        public void onItemClick(Approval approval) {
            ClueDetailFragmentArgs.Builder builder=new ClueDetailFragmentArgs.Builder();
            builder.setClueId(String.valueOf(approval.getClueId()));
            boolean  isDeal=getArguments().getBoolean("isDeal");
            if(mainViewModel.isManager()){
                if(!isDeal){
                    go(null,R.id.action_approvalFragment_to_clueDetailFragment,builder.build().toBundle());
                }else {
                    go(null,R.id.action_approvalRecordFragment_to_clueDetailFragment,builder.build().toBundle());
                }
            }else {
                go(null,R.id.action_approvalFragment_to_clueDetailFragment,builder.build().toBundle());
            }


        }

        @Override
        public void onQuotation(Approval approval) {
            QuotationFragmentArgs.Builder builder=new QuotationFragmentArgs.Builder();
            builder.setId(String.valueOf(approval.getBusinessId()));
            go(null,R.id.action_approvalFragment_to_quotationFragment,builder.build().toBundle());
        }

        @Override
        public void onBooking(Approval approval) {
            AddOrderFragmentArgs.Builder builder=new AddOrderFragmentArgs.Builder();
            builder.setApproval(new Gson().toJson(approval));
            go(null,R.id.action_approvalFragment_to_addOrderFragment,builder.build().toBundle());
        }
    };



}
